//
//  Note.m
//  Assignment3Temp
//
//  Created by zslocum on 3/9/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "Note.h"
#import <stdlib.h>

@implementation Note
- (Note*) newNoteTitle: (NSString*)ti andText: (NSString*)te{
	self = [super init];
	if(self) {		
	title = ti;
	text = te;
	}
	//need to set date variables still
	return self;
}
- (Note*) newNoteTextOnly: (NSString*)te {
	self = [super init];
	if(self) {		
		text = te;
		title = arcr4random_stir();
	}
	
	//need to set title to random and set date vars
	return self;
}
- (Note*) loadNote: (NSString*)ti {
	self = [super init];
	if(self) {
	[self retrieveNoteFromDisk: ti];
	}
	return self;
	}
	

- (NSString*) getTitle {
	return title;
}
- (int) getDate {
	return date;
}
- (NSString*) getText {
	return text;
}
- (int) getModifiedDate {
	return modifiedDate;
}
- (bool)isDeleted {
	return deleted;
}

- (void) setText:(NSString *)te {
	text = te;
}
- (void) setTitle:(NSString *)ti {
	title = ti;
}
- (void) save {
//need to setup timestamps
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString* documentsDirectory = [paths objectAtIndex:0];
documentsDirectory = [documentsDirectory stringByAppendingPathComponent: @"/notes"]    
NSString* leafname = [title stringByAppendingFormat: @".note" ]; 
NSString* filenameStr = [documentsDirectory stringByAppendingPathComponent:leafname];
NSString* saveData = [[[date stringByAppendingFormat: @"/&&&/"] stringByAppendingFormat: @"/&&&/"] stringByAppendingFromat: text];
[text writeToFile: filenameStr atomically: FALSE encoding:NSUTF8StringEncoding error:&error];
//should have file with date/&&&/moddate/&&&/text
	return;
}
- (void) parseFileIntoNote: (NSString*)filetext {
NSArray *components = [filetext componentsSeperatedByString:@"/&&&/"];
date = [components objectAtIndex: 0);
modifiedDate = [components objectAtIndex:1];
text = [components objectAtIndex: 2];
}
- (void) retrieveNoteFromDisk: (NSString*)filename {
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString* documentsDirectory = [paths objectAtIndex:0];
documentsDirectory = [documentsDirectory stringByAppendingPathComponent: @"/notes"]    
NSString* filenameStr = [documentsDirectory stringByAppendingPathComponent:filename];
NSFileHandle *file;
NSData *databuffer;
file = [NSFileHandle fileHandleForReadingAtPath: filenameStr];
if (file == nil)
        NSLog(@"Failed to open file");
databuffer = [file readDataToEndOfFile];
[parceFileIntoNote: [NSString stringWithUTF8String:[responseData bytes]]];
title = [[filename componentsSeparatedByString:@"."] objectAtIndex: 0);
[file closeFile];
}

- (void) deleteIt {
NSFileManager *filemgr;

filemgr = [NSFileManager defaultManager];
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString* documentsDirectory = [paths objectAtIndex:0];
documentsDirectory = [documentsDirectory stringByAppendingFormat: @"/notes"]    
NSString* leafname = [title stringByAppendingFormat: @".note" ]; 
NSString* filenameStr = [documentsDirectorystringByAppendingPathComponent:leafname];
if ([filemgr removeItemAtPath: filenameStr error: NULL]  == YES){
        NSLog (@"Delete successful");
        deleted = true;
    }
else
        NSLog (@"Delete failed");

	
}
@end
